package com.gsafety.cloudframework.api.oauth.cfg;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
	
//	@Bean
//	public ResourceServerTokenServices tokenService() {
//	   RemoteTokenServices tokenServices = new RemoteTokenServices();
//	   tokenServices.setClientId("2c9194445f8a0b26015f8a1123da0002");
//	   tokenServices.setClientSecret("OIVlaGT82fX8YeBUjJShw9HOnsPEn1gRg2qHB5AWJFnidMyjjbZnp0Ra6MRbdcvL");
//	   tokenServices.setCheckTokenEndpointUrl("http://127.0.0.1:8775/oauth/check_token");
//	   return tokenServices;
//	}
	
//	@Bean
//    @Override
//    protected UserDetailsService userDetailsService(){
//        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//        manager.createUser(User.withUsername("user_1").password("123456").authorities("USER").build());
//        manager.createUser(User.withUsername("user_2").password("123456").authorities("USER").build());
//        return manager;
//    }
	
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication()
              .withUser("user_1").password("123456").authorities("USER")
              .and()
              .withUser("user_2").password("123456").authorities("USER");
  }
	
  @Bean
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
      AuthenticationManager manager = super.authenticationManagerBean();
      return manager;
  }
	
	 @Override
	 protected void configure(HttpSecurity http) throws Exception {
	        // @formatter:off
	        http
	            .requestMatchers().anyRequest()
	            .and()
	                .authorizeRequests()
	                .antMatchers("/oauth/*").permitAll()
	                .and()
	                .authorizeRequests()
	                .antMatchers("oauth/check_token/*").permitAll();
	        // @formatter:on
	 }
	
}
